package com.atguigu.test

import java.text.SimpleDateFormat
import scala.io.Source

object Test3 {
  def main(args: Array[String]): Unit = {
    //1.获取数据连接
    val list = Source.fromFile("datas//小时登录次数").getLines().toList
    answer(list)
  }
  //用户小时登陆最多多少次
  def answer(list: List[String]): Unit ={
    //1.列裁剪
    val list1 = list.map(line => {
      val arr = line.split(",")
      val user = arr.head
      val time = arr.last
      //1.1定义日期格式
      val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
      //1.2解析日期
      val time1 = formatter.parse(time).getTime
      (user, time1)
    })

    //2.遍历左表,将左表数据与右表进行对比
    list1.map{
        case (user,time)=>
        val list2 = list1.filter {
          case (ruser, rtime) => user == ruser && rtime - time <= 3600000 && rtime - time >= 0
        }
          (user,list2.size)
      }
    //3.用户分组
      .groupBy{
        case(user,num)=>user
      }
        /*
        map{
          (a,(a,4)),(b,(b,5))
        }
         */
      .map{
        case (user,list)=>
          val max = list.maxBy(_._2)
          (user,max._2)
          }.foreach(println)




  }
}
